package cn.good.biz.controller;


import cn.good.biz.service.GDIssueService;
import cn.good.dto.Dto;
import cn.good.dto.DtoUtil;

import cn.good.utils.EmptyUtils;
import cn.good.vo.IssueInfoVO;
import cn.good.vo.IssueListVO;
import cn.good.vo.IssueSaveVO;
import cn.good.vo.IssueSearchVO;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RequestMapping("/doc/issue")
@RestController
public class IssueController {

    @Resource
    private GDIssueService gdIssueService;

    /**
     * 问题保存，根据用户id
     * @param issueSaveVO
     * @return
     */
    @ApiOperation(value="保存问题",httpMethod = "POST",
            protocols = "HTTP", produces = "application/json",
            response = Dto.class,notes="保存问题")
    @RequestMapping(value="/save",method= RequestMethod.POST,produces = "application/json")
    public Dto saveIssue(@RequestBody IssueSaveVO issueSaveVO){
        if(EmptyUtils.isEmpty(issueSaveVO.getUserId())){
            return DtoUtil.returnFail("用户id不能为空！","10000");
        }
        if(EmptyUtils.isEmpty(issueSaveVO.getContent())){
            return DtoUtil.returnFail("内容不能为空!","10002");
        }
        try {
            if (gdIssueService.saveIssue(issueSaveVO))
                return DtoUtil.returnSuccess();

        } catch (Exception e) {
            e.printStackTrace();
            return DtoUtil.returnFail("保存失败","10003");
        }
       return DtoUtil.returnFail("保存失败","10003");
    }

    /**
     * 查询问题列表
     * @param issueSearchVO
     * @return
     */
    @ApiOperation(value="查询问题列表",httpMethod = "POST",
            protocols = "HTTP", produces = "application/json",
            response = Dto.class,notes="查询问题列表")
    @RequestMapping(value="/search",method= RequestMethod.POST,produces = "application/json")
    public Dto searchIssueList(@RequestBody IssueSearchVO issueSearchVO,Integer pageNo){

        IssueListVO listVO =null;
        try {
         listVO = gdIssueService.searchIssueList(issueSearchVO,pageNo);
        } catch (Exception e) {
            e.printStackTrace();
            return DtoUtil.returnFail("查询失败","10004");
        }
        return DtoUtil.returnDataSuccess(listVO);
    }

    /**
     * 根据id查询问题
     * @param id
     * @return
     */
    @ApiOperation(value="根据id查询问题",httpMethod = "POST",
            protocols = "HTTP", produces = "application/json",
            response = Dto.class,notes="根据id查询问题")
    @RequestMapping("/issueInfo/{id}")
    public Dto searchIssue(@PathVariable Integer id){
        try {
            IssueInfoVO issueInfoVO = gdIssueService.searchIssueInfo(id);
            return DtoUtil.returnDataSuccess(issueInfoVO);
        } catch (Exception e) {
            e.printStackTrace();
            return DtoUtil.returnFail("查询失败","10006");
        }

    }

}
